﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Editor.Old
{
    [XmlInclude(typeof(PageLabel)), XmlInclude(typeof(PageImageBox)), XmlInclude(typeof(PageHideButton))]
    public abstract class PageControl : ICloneable
    {
        private bool selected;
        
        public string Name { get; set; }
        public string Text { get; set; }
        public bool Visible { get; set; }
        public float TextFontSize { get; set; }
        public Size Size { get; set; }
        public Point Location { get; set; }

        [XmlIgnore]
        public abstract string NamingPrefix { get; }

        [XmlIgnore]
        public static PageControl SelectedControl { get; set; }

        [XmlIgnore]
        public bool Selected 
        {
            get { return selected; }
            set
            {
                selected = value;
                onSelectedChanged();
            }
        }

        [XmlIgnore]
        public Color BackColor { get; set; }

        [XmlIgnore]
        public Color ForeColor { get; set; }

        [XmlElement("BackColor")]
        public int BackColorAsArgb
        {
            get { return BackColor.ToArgb();  }
            set { BackColor = Color.FromArgb(value); }
        }

        [XmlElement("ForeColor")]
        public int ForeColorAsArgb
        {
            get { return ForeColor.ToArgb(); }
            set { ForeColor = Color.FromArgb(value); }
        }

        [XmlIgnore]
        public Control InnerControl { get; set; }

        [XmlIgnore]
        protected PropertiesFormBase PropertiesForm { get; set; }

        public PageControl()
        {
            PropertiesForm = generatePropertiesForm();
            InnerControl = generateInnerControl();
            InnerControl.Click += new EventHandler(InnerControl_Click);
            InnerControl.BringToFront();
        }

        protected abstract Control generateInnerControl();

        void InnerControl_Click(object sender, EventArgs e)
        {
            Select();
        }

        public void Select()
        {
            SelectedControl = this;
            Selected = true;
        }

        public void Deselect()
        {
            Selected = false;
            SelectedControl = null;
        }

        public void BringToFront()
        {
            InnerControl.BringToFront();
        }

        private void onSelectedChanged()
        {
            if (PropertiesForm.IsDisposed)
            {
                EditorForm.PropertiesForm.Visible = false;
                EditorForm.PropertiesForm = null;
                PropertiesForm = generatePropertiesForm();
            }

            PropertiesForm.Visible = Selected;
            EditorForm.ChangePropertiesForm(PropertiesForm);
        }

        public override string ToString()
        {
            return Name;
        }

        protected abstract PropertiesFormBase generatePropertiesForm();
        
        public abstract void UpdateInnerControl();
        public abstract object Clone();
    }
}
